package org.dromara.third.convert.overhaulInfo.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.OverhaulInfo;
import org.dromara.system.mapper.OverhaulInfoMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.overhaulInfo.dto.OverhaulInfoDTO;
import org.dromara.third.convert.overhaulInfo.validator.OverhaulInfoValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertOverhaulInfoController {

    @Autowired
    private OverhaulInfoMapper overhaulInfoMapper;

    @PostMapping("/overhaulInfo")
    @Transactional
    public R<Void> addOverhaulInfo(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        OverhaulInfoDTO overhaulInfoDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            overhaulInfoDTO = JSONObject.parseObject(decrypt, OverhaulInfoDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, overhaulInfoDTO, new OverhaulInfoValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        OverhaulInfo overhaulInfo = assembleOverhaulInfoData(orgCode, overhaulInfoDTO);
        int count = overhaulInfoMapper.countWithOutDelFlag(overhaulInfo.getId());
        if (count == 0) {
            overhaulInfoMapper.insert(overhaulInfo);
        } else {
            overhaulInfoMapper.updateWithOutDelFlag(overhaulInfo);
        }
        return R.ok();
    }

    private OverhaulInfo assembleOverhaulInfoData(String orgCode, OverhaulInfoDTO dto) {
        OverhaulInfo overhaulInfo = new OverhaulInfo();
        try {
            overhaulInfo.setId(dto.getId());
            overhaulInfo.setHazardCode(dto.getHazardCode());
            overhaulInfo.setRiskUnitId(dto.getRiskUnitId());
            overhaulInfo.setStopStartTime(DateUtils.parseDate(dto.getStopStartTime()));
            overhaulInfo.setStopEndTime(DateUtils.parseDate(dto.getStopEndTime()));
            overhaulInfo.setStopReason(dto.getStopReason());
            overhaulInfo.setDelFlag(Long.parseLong(dto.getDelFlag()));
            overhaulInfo.setCreateDept(Long.parseLong(dto.getCreateDept()));
            overhaulInfo.setCreateBy(Long.parseLong(dto.getCreateBy()));
            overhaulInfo.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            overhaulInfo.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            overhaulInfo.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            overhaulInfo.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装风险单元停工检修信息异常");
        }
        return overhaulInfo;
    }
}
